function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}

$(function () {

    $(".base_info").submit(function (e) {
        e.preventDefault()
        // alert(111111)
        var signature = $("#signature").val()
        var nick_name = $("#nick_name").val()
        var gender = $('input[name="gender"]:checked').val()

        if (!nick_name) {
            alert('请输入昵称')
            return
        }
        if (!gender) {
            alert('请选择性别')
        }

        // TODO 修改用户信息接口
        //准备数据
        var params = {
            signature: signature,
            nick_name: nick_name,
            gender : gender
        }
        $.ajax({
            url: "/user/base_info",
            method: "POST",
            contentType: "application/json",
            data: JSON.stringify(params),
            dataType: "json",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            success: function (ret) {
                if (ret.errno == 0){
                    //表示成功
                    //修改当前的内容
                    $('.user_center_name', parent.document).html(params['nick_name'])
                    $('#nick_name', parent.document).html(params['nick_name'])
                    $('#nick_name', parent.document).attr("value", params['nick_name'])
                    $('.input_sub').blur()

                }else {
                    alert(ret.errmsg)
                }
            }
        })

    })
})